<?php
// +----------------------------------------------------------------------
// |     【ClothColor】Controller 控制器
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\admin\controller\cloth;

use think\facade\Request;
use app\common\service\ClothColor as S;
use app\common\model\ClothColor as M;
use app\common\model\ClothMain as C;
use app\common\model\ClothType as CT;

class Color extends  \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck','AdminPermission'];
	
	//初始化
	public function initialize(){
		parent::initialize();
		
		$this->assign('controller_title','面料颜色');
	}

    // 列表
    public function index(){
        if (Request::isAjax()) {
            return $this->getJson(M::getList([['uuid','=',$this->uuid]]));
        }
		
		$pid=I('pid',0);
		if($pid<=0) return toFalse(111);
		
        return $this->fetch('',[
			'pid'=>$pid,
		]);
    }

    // 添加
    public function add(){
        if (Request::isAjax()) {
			//追加uuid
			$data=array_merge(Request::post(),['uuid'=>$this->uuid]);
            return $this->getJson(S::goAdd($data));
        }
		
		$pid=I('pid',0);
		if($pid<=0) return toFalse(111);
		$cloth=C::info($pid);
		$model=[
			'pid'=>$pid,
			'number'=>$cloth['number'],
			'onumber'=>$cloth['onumber'],
		];
		
        return $this->fetch('edit',[
			'action_up'=>false,
			'model'=>$model,
			'options'=>config_cache_file('options'),
		]);
    }

    // 编辑
    public function edit($id){
        if (Request::isAjax()) {
            return $this->getJson(S::goEdit(Request::post(),['id'=>$id,'uuid'=>$this->uuid]));
        }
		
		$model=M::info(['uuid'=>$this->uuid,'id'=>$id]);
		if(!$model) $this->getJson(102);
        
		return $this->fetch('',[
			'action_up'=>true,
			'model' => $model,
			'options'=>config_cache_file('options'),
		]);
    }

    // 状态
    public function status($id){
        return $this->getJson(S::goStatus(Request::post(),['id'=>$id,'uuid'=>$this->uuid]));
    }

    // 删除
    public function remove($id){
        return $this->getJson(S::goRemove(['id'=>$id,'uuid'=>$this->uuid]));
    }

    // 批量删除
    public function batchRemove(){
        return $this->getJson(S::goBatchRemove([
				['id','in',Request::post('ids')],
				['uuid','=',$this->uuid],
			]));
    }

    // 回收站
    public function recycle(){
        if (Request::isAjax()) {
            return $this->getJson(S::goRecycle([['uuid','=',$this->uuid]]));
        }
        return $this->fetch();
    }
	
    // 设置某个值
    public function setValue(){
        if (Request::isAjax()) {
            return $this->getJson(S::setFieldValue(Request::post(),['id'=>I('post.id'),'uuid'=>$this->uuid]));
        }
        return $this->fetch();
    }
	
	//更新颜色RGB
	public function upColorRGB(){
		if (Request::isAjax()) {
			$p=I('post.p',1,'intval');
			$data=M::upRGB([['uuid','=',$this->uuid],['val','<>','']],$p);
			return $this->getJson(['msg'=>'更新成功！','data'=>['page'=>$data]]);
		}else{
			return $this->getJson(404);
		}
	}
	
	// 查询颜色
	public function find(){		
		if (Request::isAjax()){	
			$keyword=I('keyword','');
			$number=I('number','');
			$colorid=I('colorid','');
			$tid=I('tid',0,'intval');
			$range=I('range',10,'intval');
			$T=[];$hex='';$hex_result=[];$where='a.uuid='.$this->uuid;
			
			if($tid>0) $where.=' and b.tid='.$tid;

			//查询某个产品颜色的相近色
			if(intval($number)!=0 && intval($colorid)!=0){
				$findwhere="number='".$number."' and colorid=".$colorid.' and uuid='.$this->uuid;
				$T=M::where($findwhere)->find();
				if(!$T){
					return $this->getJson(['code'=>201,'msg'=>'该面料颜色不存在，或者未录入！']);
				}else{
					if($T['val']!=''){
						$hex="#".$T['val'];
						$hex_result=[
							'ptHex'=>$hex,
							'ptRgb'=>'RGB('.$T['red'].','.$T['green'].','.$T['blue'].')',
							'ptCode'=>'无',
						];
					}else{
						return $this->getJson(['code'=>201,'msg'=>'该面料颜色参数值为空！']);
					}
				}
			//查询潘通色号和HEX颜色
			}else if($keyword!=''){
				
				$pattern='/^#([a-fA-F\d]{6}|[a-fA-F\d]{3})$/';
				preg_match($pattern,$keyword,$match);
				if(count($match)>0){		//查询电子色
					$rgb=hex2rgba($keyword);	
					$hex=$keyword;
					$hex_result=[
						'ptHex'=>$hex,
						'ptRgb'=>'RGB('.$rgb[0].','.$rgb[1].','.$rgb[2].')',
						'ptCode'=>'无',
					];
				}else{				//查询潘通色卡
					$url='https://www.qtccolor.com/findcolor.aspx';
					$get=[
						'ajax'=>1,
						'word'=>$keyword,
					];
					$get_data=curlGet($url,$get,4);
					if(count($get_data)>0){
						$hex_result=[
							'ptHex'=>$get_data[0]['ColorlibraryItems'][0]['ColorValue_Hex'],
							'ptRgb'=>$get_data[0]['ColorlibraryItems'][0]['ColorValue'],
							'ptCode'=>$get_data[0]['ColorlibraryItems'][0]['ColorCode'],
						];
						$hex=$hex_result['ptHex'];
					}else{
						return $this->getJson(['code'=>201,'msg'=>'该潘通色号不存在！']);
					}
				}
			}else{
				return $this->getJson(['code'=>201,'msg'=>'搜索内容不能为空！']);
			}
			$T=$this->analogyColor($hex,$range,$where,200);
			$data=[
				'list'=>$T,
				'count'=>count($T),
				'hex'=>$hex_result,
			];
			return $this->getJson(['code'=>0,'data'=>$data]);
		}
		
		return $this->fetch('',[
			'type'=>CT::listData(),
		]);
	}
}
